<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
    </el-breadcrumb>
    <div class="commod_search_container">
      <div>搜索：</div>
      <div>
        <el-input v-model="departmentName" placeholder="输入部门名称"></el-input>
      </div>
      <div>
        <el-input v-model="departmentCode" placeholder="输入部门代码"></el-input>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="onSearch">搜索</el-button>
      </div>
      <div><el-button type="primary" icon="el-icon-plus">添加</el-button></div>
    </div>

    <a-table
      :columns="columns"
      :data-source="showlist"
      bordered
      :pagination="{
        defaultPageSize: 5,
        total: showlist.length,
      }"
    >
      <template slot="operation" slot-scope="text, record">
        <a-button type="primary" ghost @click="onClickDetails(record)">
          编辑
        </a-button>
        <a-button type="danger" ghost @click="onClickDel(record)">
          删除
        </a-button>
      </template>
    </a-table>

    <!--                    修改数据-->
    <div>
      <a-modal v-model="visible" title="修改" @ok="handleOk">
        <a-form-model ref="ruleForm" :model="message">
          <a-form-model-item has-feedback label="用户名">
            <a-input v-model="message.name" type="text" autocomplete="off" />
          </a-form-model-item>
          <a-form-model-item has-feedback label="地址">
            <a-input v-model="message.dz" type="text" autocomplete="off" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      columns: [
        {
          title: "Id",
          dataIndex: "id",
          key: "id",
        },
        {
          title: "部门名称",
          dataIndex: "name",
          key: "name",
        },
        {
          title: "部门代码",
          dataIndex: "category",
          key: "category",
        },
        {
          title: "手机号",
          dataIndex: "phone",
          key: "phone",
        },
        {
          title: "地址",
          dataIndex: "address",
          key: "address",
        },
        {
          title: "操作",
          dataIndex: "operation",
          key: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      list: [],
      showlist: [],
      departmentName: "",
      departmentCode:"",
      message: {
        name: "",
        dz: "",
        id: 0,
      },
      visible: false,
    };
  },
  mounted() {
    this.$axios.get("http://localhost:8080/shopList.json").then((res) => {
      // console.log(res.data)
      this.list = res.data;
      this.showlist = res.data;
    });
  },
  methods: {
    //搜索
    onSearch() {
      if (this.departmentName.length == 0 || this.departmentCode.length==0) {
        return this.$message.error("搜索内容不能为空");
      }

      this.showlist = [];

      this.list.map((el) => {
        if (el.name.includes(this.departmentName) && el.category.includes(this.departmentCode)) {
          this.showlist.push(el);
        }
      });
    },

    //修改
    onClickDetails(record) {
      //显示弹出框
      this.visible = true;

      //定义一个message数据，让他们保持一致
      this.message.name = record.name;
      this.message.id = record.id;
      this.message.dz = record.address;
    },
    //修改成功OK
    handleOk() {
      //隐藏弹出框
      this.visible = false;

      //遍历数组，如果id相等，。和修改后的名字和地址保持一致
      this.showlist.map((el) => {
        if (el.id == this.message.id) {
          el.name = this.message.name;
          el.address = this.message.dz;
        }
      });
    },

    //删除
    onClickDel(record) {
      console.log(record);
      var index = this.showlist.findIndex((el) => {
        return el.id == record.id;
      });
      if (index != -1) {
        this.showlist.splice(index, 1);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.commod_search_container {
  margin: 10px 0;
  width: 70%;
  height: 50px;
  // background: red;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
}
img {
  width: 50px;
  height: 50px;
}
</style>